﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Betfairer
{
    public class Scanner
    {
        private CommonSelector _selector;

        private int _sportEventId;
        private int _listenedMarketsLimit;

        private List<Market> _listenedMarkets;
        private List<Market> _examinedMarkets;

        public void Start(int sportEventId, int listenedMarketsLimit, int scanDelayMs)
        {
            _selector = new CommonSelector(); // параметры передавать здесь, а не при вызове метода.

            _sportEventId = sportEventId;
            _listenedMarketsLimit = listenedMarketsLimit;

            _listenedMarkets = new List<Market>();
            _examinedMarkets = new List<Market>();

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.AutoReset = true;
            timer.Interval = scanDelayMs;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

            // run timer
            timer.Enabled = true;
            Console.WriteLine("[SCANNER] Scanner has been started.");
        }

        private void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine(_listenedMarkets.Count);
            for (int i = 0; i < _listenedMarkets.Count; ++i)
            {
                ListenMarket(_listenedMarkets[i]);
            }

            if (_listenedMarkets.Count < _listenedMarketsLimit)
            {
                ScanMarkets();
            }

            //Console.WriteLine("Blacklist length: " + _selector.BlackList.Count);
        }

        private void ScanMarkets()
        {

            List<Market> markets = Global.Instance.GetMarkets(_sportEventId, DateTime.UtcNow.Date, DateTime.UtcNow.Date.AddDays(2));
            Console.WriteLine("[SCANNER] Retrieved {0} markets.", markets.Count);
            foreach (Market market in markets)
            {

                if (!_listenedMarkets.Contains(market) && !_examinedMarkets.Contains(market))
                {
                    if (_selector.SergeSelect(market, "Match Odds"))
                    {
                        lock (_listenedMarkets)
                        {
                            _listenedMarkets.Add(market);
                        }
                        System.IO.File.AppendAllText(String.Format(@"D:\dev\projects\neural\data\{0}.txt", market.MarketId), market.ToString() + Environment.NewLine);
                        break;
                    }
                }
            }

        }

        private void ListenMarket(Market market)
        {
            if (Utils.Now + 10 * 60 * 1000 >= market.EventDateMs)
            {
                lock (_listenedMarkets)
                {
                    _listenedMarkets.Remove(market);
                    _examinedMarkets.Add(market);
                }

                return;
            }

            Console.WriteLine("Getting for id: " + market.MarketId);
            System.IO.File.AppendAllText(String.Format(@"D:\dev\projects\neural\data\{0}.txt", market.MarketId), Utils.CreateRecord(market));
        }
    }
}
